package cn.pconline.autoclub.web.app;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.pconline.autoclub.entity.CloudLog;
import cn.pconline.autoclub.entity.Club;
import cn.pconline.autoclub.entity.ClubMember;
import cn.pconline.autoclub.entity.MemberApplication;
import cn.pconline.autoclub.entity.User;
import cn.pconline.autoclub.exception.ApiException;
import cn.pconline.autoclub.service.CloudLogService;
import cn.pconline.autoclub.service.ClubService;
import cn.pconline.autoclub.service.MemberService;
import cn.pconline.autoclub.service.MessageService;
import cn.pconline.autoclub.service.UserService;
import cn.pconline.autoclub.util.EncodeUtils;
import cn.pconline.autoclub.util.Pager;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.autoclub.util.app.AppControllerStatic;
import cn.pconline.autoclub.web.TokenController;
import cn.pconline.autoclub.web.UserAuth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * @author zhouruxuan
 * @date 2015年2月4日 上午11:38:16
 */
@Controller
@RequestMapping(value = "/appapi")
public class AppMemberController {
	
	@Autowired
    private UserAuth userAuth;
	@Autowired
    private ClubService clubService;
	@Autowired
    private MemberService memberService;
	@Autowired
    private MessageService messageService;
	@Autowired
    private TokenController tokenController;
	@Autowired
    private UserService userService;
    @Autowired
    private CloudLogService cloudLogService;
	
	private static final Logger log = LoggerFactory.getLogger(AppMemberController.class);
    /**拼接用户头像图片,域名：http://i6.3conline.com/images/upload/upc/face/ */
    private static final long AUTHORPHOTO_URL_I6 = 6L;
	

	/**
     * 2.14车友会成员管理接口(旧版） 接口URL:/member/operation.do
     * type:操作类型，1--转让会长，2--设置副会长，3--解除职务，4--移除车友会 5--取消审核中车友会申请
     *
     * 拆分为独立的接口----转让会长接口
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/member/transferClubAdmin.do", method = RequestMethod.POST)
    public void transferClubAdmin(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取参数
        long clubId = AppControllerStatic.paramClubId(request);
        long targetId = AppControllerStatic.paramUserId(request);
        String callback = AppControllerStatic.paramCallback(request);

        JSONObject json = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录，请先登录", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断车友会是否合法
        Club club = clubService.getValidClub(clubId);
        if (club == null) {
            AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断是否为会长
        if (!memberService.isClubOwner(user.getUid(), clubId)) {
            AppControllerStatic.printErrorMsg(response, "您不是该车友会会长，只有会长才能进行该操作哦", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长不可以对自己进行操作
        if(user.getUid() == targetId) {
        	AppControllerStatic.printErrorMsg(response, "你已经是会长，不需要转换", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长信息，目标会长信息
        ClubMember operator = memberService.findMember(user.getUid(), clubId);
        ClubMember targetMember = memberService.findMember(targetId, clubId);
        if (operator == null) {
            AppControllerStatic.printErrorMsg(response, "非车友会成员不允许操作", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        if (targetMember == null) {
            AppControllerStatic.printErrorMsg(response, "只可以操作本车友会成员", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //进行转让
        int resultCode = memberService.adminAssignment(operator, targetMember, ClubMember.ADMIN);
        // 转让失败
        if (resultCode == 0) {
            AppControllerStatic.printErrorMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //转让成功
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 2.14车友会成员管理接口(旧版） 接口URL:/member/operation.do
     * type:操作类型，1--转让会长，2--设置副会长，3--解除职务，4--移除车友会 5--取消审核中车友会申请
     *
     * 拆分为独立的接口----设置副会长接口
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/member/setClubViceAdmin.do", method = RequestMethod.POST)
    public void setClubViceAdmin(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取参数
        long clubId = AppControllerStatic.paramClubId(request);
        long targetId = AppControllerStatic.paramUserId(request);
        String callback = AppControllerStatic.paramCallback(request);

        JSONObject json = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录，请先登录", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断车友会是否合法
        Club club = clubService.getValidClub(clubId);
        if (club == null) {
            AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断是否为会长
        if (!memberService.isClubOwner(user.getUid(), clubId)) {
            AppControllerStatic.printErrorMsg(response, "您不是该车友会会长，只有会长才能进行该操作哦", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长不可以设置自己为副会长
        if (user.getUid() == targetId) {
            AppControllerStatic.printErrorMsg(response, "你是会长，不可以将自己转换为副会长", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长信息，目标副会长信息
        ClubMember operator = memberService.findMember(user.getUid(), clubId);
        ClubMember targetMember = memberService.findMember(targetId, clubId);
        if (targetMember == null) {
            AppControllerStatic.printErrorMsg(response, "只可以操作本车友会成员", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //不可以设置已经是副会长的成员
        if (targetMember.isViceAdmin()) {
            AppControllerStatic.printErrorMsg(response, "已经是副会长，不需要转换", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //进行设置副会长
        int resultCode = memberService.adminAssignment(operator, targetMember, ClubMember.VICE_ADMIN);
        //设置副会长失败
        if (resultCode == 0) {
            AppControllerStatic.printErrorMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //设置副会长成功
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 2.14车友会成员管理接口(旧版） 接口URL:/member/operation.do
     * type:操作类型，1--转让会长，2--设置副会长，3--解除职务，4--移除车友会 5--取消审核中车友会申请
     *
     * 拆分为独立的接口----解除职务接口
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/member/removalClubOffice.do", method = RequestMethod.POST)
    public void removalClubOffice(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取参数
        long clubId = AppControllerStatic.paramClubId(request);
        long targetId = AppControllerStatic.paramUserId(request);
        String callback = AppControllerStatic.paramCallback(request);

        JSONObject json = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
        	AppControllerStatic.printErrorMsg(response, "用户未登录，请先登录", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断车友会是否合法
        Club club = clubService.getValidClub(clubId);
        if (club == null) {
            AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断是否为会长
        if (!memberService.isClubOwner(user.getUid(), clubId)) {
            AppControllerStatic.printErrorMsg(response, "您不是该车友会会长，只有会长才能进行该操作哦", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长不可以自己解除自己
        if(user.getUid() == targetId) {
        	AppControllerStatic.printErrorMsg(response, "你是会长，请先转让会长职务再退出车友会", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长信息，被解除职务人员信息
        ClubMember operator = memberService.findMember(user.getUid(), clubId);
        ClubMember targetMember = memberService.findMember(targetId, clubId);
        if (operator == null) {
            AppControllerStatic.printErrorMsg(response, "非车友会成员不允许操作", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        if (targetMember == null) {
            AppControllerStatic.printErrorMsg(response, "只可以操作本车友会成员", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //进行解除职务
        int resultCode = memberService.adminAssignment(operator, targetMember, ClubMember.NON_ADMIN);
        //解除职务失败
        if (resultCode == 0) {
            AppControllerStatic.printErrorMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //解除职务成功
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }

    /**
     * 2.14车友会成员管理接口(旧版） 接口URL:/member/operation.do
     * type:操作类型，1--转让会长，2--设置副会长，3--解除职务，4--移除车友会 5--取消审核中车友会申请
     *
     * 拆分为独立的接口----移除车友会接口（被会长进行移出车友会）
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/member/removeClub.do", method = RequestMethod.POST)
    public void removeClub(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取参数
        long clubId = AppControllerStatic.paramClubId(request);
        long targetId = AppControllerStatic.paramUserId(request);
        String callback = AppControllerStatic.paramCallback(request);

        JSONObject json = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录，请先登录", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断车友会是否合法
        Club club = clubService.getValidClub(clubId);
        if (club == null) {
            AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断是否为会长
        if (!memberService.isClubOwner(user.getUid(), clubId)) {
            AppControllerStatic.printErrorMsg(response, "您不是该车友会会长，只有会长才能进行该操作哦", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长不可以自己解除自己
        if(user.getUid() == targetId) {
        	AppControllerStatic.printErrorMsg(response, "你是会长，请先转让会长职务再退出车友会", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长信息，被移除车友会的人员信息
        ClubMember operator = memberService.findMember(user.getUid(), clubId);
        ClubMember targetMember = memberService.findMember(targetId, clubId);
        if (operator == null) {
            AppControllerStatic.printErrorMsg(response, "非车友会成员不允许操作", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        if (targetMember == null) {
            AppControllerStatic.printErrorMsg(response, "只可以操作本车友会成员", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //移除车友会  
        int resultCode = memberService.expelMember(operator, targetMember);
        //移除车友会失败
        if (resultCode == 0) {
            AppControllerStatic.printErrorMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //发送消息给被移出车友会的用户
        String clubIndexUrl = messageService.assembleClubUrl(club.getClubUrl(), club.getName());
        long deleMemberId = targetMember.getUid();
        String sendMsg = String.format(MessageService.FIRED_CLUB, clubIndexUrl);
        sendMessageToUser(deleMemberId, sendMsg);
        //调用融云退出群API
        callRongClubQuitClub(deleMemberId, clubId);
        //移除车友会成功
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }
    
    /**
     * 2.14车友会成员管理接口(旧版） 接口URL:/member/operation.do
     * type:操作类型，1--转让会长，2--设置副会长，3--解除职务，4--移除车友会 5--取消审核中车友会申请
     *
     * 拆分为独立的接口----退出车友会接口(用户自己进行退出）
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/member/quitClub.do", method = RequestMethod.POST)
    public void quitClub(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取参数
        long clubId = AppControllerStatic.paramClubId(request);
        String callback = AppControllerStatic.paramCallback(request);

        JSONObject json = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录，请先登录", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断车友会是否合法
        Club club = clubService.getValidClub(clubId);
        if (club == null) {
        	AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //会长信息，退出车友会的人员信息
        ClubMember operator = memberService.findMember(user.getUid(), clubId);
        if (operator == null) {
            AppControllerStatic.printErrorMsg(response, "非车友会成员不允许操作", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //如果用户本身是会长，不可以退出车友会，必需进行会长转让才可以退出车友会
        if (operator.getAdminType() == ClubMember.ADMIN) {
            AppControllerStatic.printErrorMsg(response, "请转让会长后再退出车友会", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }

        //退出车友会  
        int resultCode = memberService.expelMember(operator, operator);
        //退出车友会失败
        if (resultCode == 0) {
            AppControllerStatic.printErrorMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //发送消息给被移出车友会的用户
        String clubIndexUrl = messageService.assembleClubUrl(club.getClubUrl(), club.getName());
        long deleMemberId = operator.getUid();
        String sendMsg = String.format(MessageService.EXIT_CLUB, clubIndexUrl, user.getNickname());
        sendMessageToAdmin(clubId, sendMsg);
        //调用融云退出群API
        callRongClubQuitClub(deleMemberId, clubId);
        //退出车友会成功
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }
    
    /**
     * 2.14车友会成员管理接口(旧版） 接口URL:/member/operation.do
     * type:操作类型，1--转让会长，2--设置副会长，3--解除职务，4--移除车友会 5--取消审核中车友会申请
     *
     * 拆分为独立的接口----取消审核中车友会申请
     *
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/member/cancleApplyRecord.do", method = RequestMethod.POST)
    public void cancleApplyRecord(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setEncoding4App(request, response);
        //获取参数
        long clubId = AppControllerStatic.paramClubId(request);
        String callback = AppControllerStatic.paramCallback(request);

        JSONObject json = new JSONObject();

        //判断用户是否登录
        User user = userAuth.getUser(request, response);
        if (user == null || user.getUid() <= 0) {
            AppControllerStatic.printErrorMsg(response, "用户未登录，请先登录", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //判断车友会是否合法
        Club club = clubService.getValidClub(clubId);
        if (club == null) {
            AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //获取末审核的申请加入车友会记录
        MemberApplication memberApplication = memberService.getMemberApplication(clubId, user.getUid(), MemberApplication.STATUS_UNAUDITED);
        if (memberApplication == null) {
            AppControllerStatic.printErrorMsg(response, "找不到该申请记录", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //是否为本人操作
        if (memberApplication.getUid() != user.getUid()) {
            AppControllerStatic.printErrorMsg(response, "请本人操作", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //审核已经通过了，不可以取消
        if (memberApplication.getStatus() == MemberApplication.STATUS_PASS) {
            AppControllerStatic.printErrorMsg(response, "您已是该车友会成员", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //审核已经被驳回，不可以取消
        if (memberApplication.getStatus() == MemberApplication.STATUS_REJECT) {
            AppControllerStatic.printErrorMsg(response, "您加入车友会的申请已被拒绝，请尝试加入其他车友会", json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //取消申请
        int resultCode = memberService.deleteMemberApplicationReturn(memberApplication);
        //取消申请失败
        if (resultCode == 0) {
            AppControllerStatic.printErrorMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
            return;
        }
        //发送消息给管理员
        String clubIndexUrl = messageService.assembleClubUrl(club.getClubUrl(), club.getName());
        User targetUser = userService.findUser(memberApplication.getUid());
        String sendMsg = String.format(MessageService.CANEL_APPLY, clubIndexUrl, targetUser.getNickname());
        sendMessageToAdmin(clubId, sendMsg);
        //取消成功
        AppControllerStatic.printSuccessMsg(response, json, callback, AppControllerStatic.V1_VERSION_LATEST);
    }
    
    /**
     * 发送系统消息给用户
     *
     * @param userId
     * @param sendMsg
     */
    private void sendMessageToUser(long userId, String sendMsg) {
        if (StringUtils.isNotBlank(sendMsg) && userId > 0) {
            User targetUser = userService.findUser(userId);
            if (targetUser != null) {
                messageService.send(targetUser, sendMsg);
            }
        }
    }
    
    /**
     * 发送系统消息给车友会管理员
     *
     * @param clubId
     * @param sendMsg
     */
    private void sendMessageToAdmin(long clubId, String sendMsg) {
        List<ClubMember> listAdmin = memberService.listAdmins(clubId);
        if (listAdmin != null && !listAdmin.isEmpty() && StringUtils.isNotBlank(sendMsg)) {
            List<User> users = memberService.getUserByMember(listAdmin);
            messageService.sendMsg(users, sendMsg);
        }
    }
    
    /**
     * 用户退出车友会时，调用融云退出群接口
     *
     * @param userId
     * @param clubId
     */
    private void callRongClubQuitClub(long userId, long clubId) {
        if (userId > 0 && clubId > 0) {
            String userId_s = "";
            String clubId_s = "";
            try {
                userId_s = String.valueOf(userId);
                clubId_s = String.valueOf(clubId);
                boolean quitFlag = tokenController.QuitClub(userId_s, clubId_s);
                if (!quitFlag) {
                	//插入失败日志
                	cloudLogService.create(clubId,"",userId,CloudLog.QUIT_CLOUD);
                    log.error("callRongClubQuitClub fail 1!userId=" + userId_s + ",clubId=" + clubId_s);
                }
            } catch (ApiException exception) {
            	cloudLogService.create(clubId,"",userId,CloudLog.QUIT_CLOUD);
                log.error("callRongClubQuitClub fail 2!userId=" + userId_s + ",clubId=" + clubId_s);
            }
        }
    }
    
    /**
     * 车友会成员列表接口
     *   1.参数clubId,clubUrl不可以同时为空值；
     *   2.如果参数clubId,clubUrl都传值，以clubId为准进行查找。
     *   
     * @param request
     * @param response
     */
    @ResponseBody
    @RequestMapping(value = "/1.0/member/getMemberList.do", method = RequestMethod.GET)
    public void getMemberList(HttpServletRequest request, HttpServletResponse response) {
    	WebUtils.setEncoding4App(request, response);
        long clubId = AppControllerStatic.paramClubId(request);
        String clubUrl = AppControllerStatic.paramClubUrl(request);
        int isShowAdmin = WebUtils.paramInt(request, "isShowAdmin", 1);//1》》显示  0》》隐藏 
        String callback = AppControllerStatic.paramCallback(request);
        int pageSize = AppControllerStatic.paramPageSize(request);
        int pageNo = AppControllerStatic.paramPageNo(request);
        int xssEncode = AppControllerStatic.paramXssEncode(request);
        
        JSONObject jsonObject = new JSONObject();
        User user = userAuth.getUser(request, response);
        
        //参数检查
        if(clubId == 0 && StringUtils.isBlank(clubUrl)) {
        	AppControllerStatic.printErrorMsg(response, "参数clubId,clubUrl不可以同时为空值", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        	return;
        }
        
        Club club;
        
        //如果两个参数同时存在，以clubId为准
        if(clubId > 0) {
        	club = clubService.getValidClub(clubId);
        } else {
        	club = clubService.getClubByUrl(clubUrl);
        	if(club == null) {
        		AppControllerStatic.printErrorMsg(response, "该车友会域名不存在", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
            	return;
        	}
        	clubId = club.getClubId();
        }

        if(club == null) {
        	AppControllerStatic.printErrorMsg(response, "该车友会ID不存在", jsonObject, callback, AppControllerStatic.V1_VERSION_LATEST);
        	return;
        }
        
        //设置返回值
        jsonObject.put("clubId", clubId);
        jsonObject.put("clubUrl", club.getClubUrl());
        jsonObject.put("adminType", clubService.getClubRelationByUser(user, clubId));
        
        Pager<ClubMember> pager;
        
        //是否独立显示会长信息
        if (isShowAdmin > 0) {
        	//会长数组
            List<ClubMember> listAdmin = memberService.listAdmins(clubId);
            if (listAdmin != null && !listAdmin.isEmpty()) {
            	JSONArray jsonAdminArray = new JSONArray();
            	for(ClubMember clubMember : listAdmin) {
            		JSONObject jsonAdmin = member2Json(clubMember, xssEncode);
            		jsonAdminArray.add(jsonAdmin);
            	}
            	jsonObject.put("admins", jsonAdminArray);
            }
            
            //普通会员信息
            pager = memberService.pagerMemberByType(clubId, ClubMember.NON_ADMIN, pageNo, pageSize);
            if (pager != null && !pager.getResultList().isEmpty()) {
            	JSONArray jsonMemberArray = new JSONArray();
            	for(ClubMember clubMember : pager.getResultList()) {
            		JSONObject jsonMember = member2Json(clubMember, xssEncode);
            		jsonMemberArray.add(jsonMember);
            	}
            	jsonObject.put("members", jsonMemberArray);
            }
        } else {//会长跟普通会员一起显示
        	pager = memberService.pagerMembersByClubId(clubId, pageNo, pageSize);
            if (pager != null && !pager.getResultList().isEmpty()) {
            	JSONArray jsonMemberArray = new JSONArray();
            	for(ClubMember clubMember : pager.getResultList()) {
            		JSONObject jsonMember = member2Json(clubMember, xssEncode);
            		jsonMemberArray.add(jsonMember);
            	}
            	jsonObject.put("data", jsonMemberArray);
            }
        }
        
        
        
        //登录用户查找自己的申请加入车友会正在被审核中的记录
        if (user != null && user.getUid() > 0 && memberService.isClubNormalAdmin(user.getUid(), clubId)) {
            List<MemberApplication> listApply = memberService.listMemberApplications4Audit(clubId);
            JSONArray jsonArray = new JSONArray();
            if (listApply != null && !listApply.isEmpty()) {
                for (MemberApplication apply : listApply) {
                    JSONObject obj = new JSONObject();
                    obj.put("userId", apply.getUid());
                    obj.put("memberId", apply.getId());
                    obj.put("photo", WebUtils.face(apply.getUid(), AppControllerStatic.APP_API_AUTHORPHOTO_SIZE_50, AUTHORPHOTO_URL_I6));
                    if(xssEncode == 0) {
                    	obj.put("name", apply.getUser().getUsername());
                        obj.put("nickname", apply.getUser().getNickname());
                        obj.put("truename", apply.getUser().getTruename());
                        obj.put("province", apply.getUser().getProvince());
                        obj.put("city", apply.getUser().getCity());
                        obj.put("phone", apply.getUser().getPhone());
                    } else {
                    	obj.put("name", EncodeUtils.encodeForHTMLFilterNull(apply.getUser().getUsername()));
                        obj.put("nickname", EncodeUtils.encodeForHTMLFilterNull(apply.getUser().getNickname()));
                        obj.put("truename", EncodeUtils.encodeForHTMLFilterNull(apply.getUser().getTruename()));
                        obj.put("province", EncodeUtils.encodeForHTMLFilterNull(apply.getUser().getProvince()));
                        obj.put("city", EncodeUtils.encodeForHTMLFilterNull(apply.getUser().getCity()));
                        obj.put("phone", EncodeUtils.encodeForHTMLFilterNull(apply.getUser().getPhone()));
                    }
                    String info;
                    if (apply.getUser().getVip() == 0) {
                        info = WebUtils.car(apply.getUser().getCarInfo());
                    } else {
                        info = WebUtils.car(apply.getUser().getVipInfo());
                    }
                    obj.put("vipInfo", info);
                    obj.put("isVip", apply.getUser().getVip());
                    jsonArray.add(obj);
                }
                jsonObject.put("applys", jsonArray);
            }
        }
        
        AppControllerStatic.printSuccessMsgPager(response, jsonObject, callback, pager, AppControllerStatic.V1_VERSION_LATEST);
        
    }
    
    /**
     * 组装member的Json信息
     * @param clubMember
     * @param xssEncode
     * @return
     */
    public JSONObject member2Json(ClubMember clubMember, int xssEncode) {
    	JSONObject jsonObject = new JSONObject();
        jsonObject.put("userId", clubMember.getUid());
        jsonObject.put("memberId", clubMember.getId());
        jsonObject.put("photo", WebUtils.face(clubMember.getUid(), AppControllerStatic.APP_API_AUTHORPHOTO_SIZE_50, AUTHORPHOTO_URL_I6));
        
        if(xssEncode == 0) {
        	jsonObject.put("name", clubMember.getUser().getUsername());
            jsonObject.put("nickname", clubMember.getUser().getNickname());
            jsonObject.put("truename", clubMember.getUser().getTruename());
            jsonObject.put("province", clubMember.getUser().getProvince());
            jsonObject.put("city", clubMember.getUser().getCity());
            jsonObject.put("phone", clubMember.getUser().getUcPhone());
        } else {
        	jsonObject.put("name", EncodeUtils.encodeForHTMLFilterNull(clubMember.getUser().getUsername()));
            jsonObject.put("nickname", EncodeUtils.encodeForHTMLFilterNull(clubMember.getUser().getNickname()));
            jsonObject.put("truename", EncodeUtils.encodeForHTMLFilterNull(clubMember.getUser().getTruename()));
            jsonObject.put("province", EncodeUtils.encodeForHTMLFilterNull(clubMember.getUser().getProvince()));
            jsonObject.put("city", EncodeUtils.encodeForHTMLFilterNull(clubMember.getUser().getCity()));
            jsonObject.put("phone", EncodeUtils.encodeForHTMLFilterNull(clubMember.getUser().getUcPhone()));
        }
        String info;
        if (clubMember.getUser().getVip() == 0) {
            info = WebUtils.car(clubMember.getUser().getCarInfo());
        } else {
            info = WebUtils.car(clubMember.getUser().getVipInfo());
        }
        jsonObject.put("petrol", clubMember.getPetrol());
        jsonObject.put("vipInfo", info);
        jsonObject.put("type", clubMember.getAdminType());
        jsonObject.put("isVip", clubMember.getUser().getVip());
        
        return jsonObject;
    }
}
